<?php
namespace Rainrock\Framework\Controller;


use Rainrock\Framework\kernel\core\Controller;
use Rainrock\Framework\kernel\base\Rock;
use Rainrock\Framework\kernel\base\Request;
use Rainrock\Framework\kernel\base\Web;
use Rainrock\Framework\kernel\base\Base;
use Rainrock\Framework\kernel\base\Jiami;
use Rainrock\Framework\kernel\Html\HtmlBase;
use Rainrock\Framework\kernel\Html\HtmlLabel;
use Rainrock\Framework\kernel\Html\HtmlInput;
use Rainrock\Framework\kernel\Html\HtmlButton;
use Rainrock\Framework\kernel\Html\HtmlImage;
use Rainrock\Framework\Chajian\smsChajian;

/**
*	登录
*/
class LoginController extends Controller{
	
	public $adminurl = '';
	
	public function initController()
	{
		$this->adminurl = Base::getConfig('adminurl');
	}
	
	
	public function indexAction()
	{
		if($this->getAuth()){
			$this->location($this->adminurl);
		}else{
			if(Web::isMobile()){
				return $this->loginViewwe();
			}else{
				return $this->loginView();
			}
		}
	}
	
	public function weAction()
	{
		if($this->getAuth()){
			$this->location($this->adminurl);
		}else{
			return $this->loginViewwe();
		}
	}
	
	/**
	*	PC登录的视图界面
	*/
	public function loginView()
	{
		$html = HtmlBase::get();
		$html->setTitle(TITLE);
		$html->addJse('login');
	
		$user = HtmlLabel::get()
			->addStyle('margin-top','10px')
			->setDisplay('flex')
			->addCont(
				HtmlInput::gets()
				->setTishi('可输入帐号/手机号')
				->addStyle('flex',1)
				->addAttrs('autocomplete','off')
				->setLen(50)
				->setId('user')
		);
		
		$pass = HtmlLabel::get()
			->setDisplay('flex')
			->addCont(
				HtmlInput::gets()
				->setTishi('请输入密码')
				->setType('password')
				->addStyle('flex',1)
				->setLen(50)
				->setId('pass')
		);
		
		$yzm = HtmlLabel::get()
			->addStyle('padding-top','20px')
			//->setDisplay('none')
			->setId('yzmdiv')
			->addCss('webbtn-group')
			->addCont(
				HtmlInput::gets()
				->setTishi('手机验证码')
				->setLen(6)
				->addStyle('flex', 1)
				->setId('yzm')
			)
			->addCont(
				HtmlButton::gets()
				->addCss('webbtn-default')
				->setValue('获取验证码')
			);			

		$zhmm = HtmlLabel::get()
			->addCont(
				HtmlLabel::get('a')->addCont('找回密码')->setHref('?m=login&a=find')
			)
			/*
			->addKong()
			->addKong()
			->addCont(
				HtmlLabel::get('a')->addCont('注册')->setHref('?m=login&a=reg')
			)
			->addKong()
			->addKong()
			->addCont(
				HtmlLabel::get('a')->addCont('验证码登录')->setHref('javascript:changelogin()')
			)
			*/
			->addStyle('margin-top','5px');
			
		$btn = HtmlLabel::get()
			->addCont(
				HtmlButton::gets()
				->setValue('登录')
				->setWidth('100px')
				->setId('loginbtn')
				->setonClick('submitlogin()')
			)
			->addKong()
			->addCont(HtmlLabel::get('span')->setId('msgview'));
		
		$mdivs = HtmlLabel::get()->setAlign('left');	
		
		$logo = ''.ROOT_PATH.'/images/logo.png';
		if(file_exists($logo)){
			$mdivs->addCont(HtmlLabel::get()->setAlign('center')->addCont(HtmlImage::gets()->setSrc(''.URL.'images/logo.png')->setWidth(100)->setHeight(100,'hidden')->setTitle(TITLE)->setRadius('50%')->setonClick('js.reload()')));
		}
		
		$div001= HtmlLabel::get()->addCont($user)->addBlank(20)->addCont($pass)->setId('div001');
		
		$mdivs = $mdivs
				->addCont($div001)
				//->addCont($yzm)
				//->addCont($zhmm)
				->addBlank(20)
				->addStyle('margin','30px 80px')
				->addCont($btn);
				
		$mdivs = HtmlLabel::get()->addCont($mdivs)	
				->addCss('box')
				->setWidth(430)
				->setBackgroundColor('var(--main-bgcolor)')
				->addStyle('border','var(--border)')
				->setRadius(10)
				->addStyle('margin-top','20px')
				->addStyle('margin-bottom','20px')
				->setAlign('left');		
	
	
		$html->addBody(HtmlLabel::get()->setHeight(50,'hidden'));
		
		$tit = HtmlLabel::get()
			->addStyle('padding','10px')
			->setFontsize(30)
			->addCont(TITLE);
		$html->addBody($tit);
		
		$html->addBody($mdivs);
		$html->addFBody(PublicViews::Footer());

		return $html->show();
	}
	
	/**
	*	手机端登录
	*/
	public function loginViewwe()
	{
		$html = HtmlBase::get();
		$html->setTitle(TITLE);
		$html->addJse('login');
		
		$logo = ''.ROOT_PATH.'/images/logo.png';
		if(file_exists($logo)){
			$html->addCont(HtmlLabel::get()->addStyle('margin-top', '50px')->setAlign('center')->addCont(HtmlImage::gets()->setSrc(''.URL.'images/logo.png')->setWidth(100)->setHeight(100,'hidden')->setTitle(TITLE)->setRadius('50%')->setonClick('js.reload()')));
		}
		
		$user = HtmlInput::gets()
				->setTishi('可输入帐号/手机号')
				->addStyle('flex',1)
				->addAttrs('autocomplete','off')
				->addStyle('background','none')
				->addStyle('border','none')
				->addStyle('line-height','30px')
				->setLen(50)
				->setId('user');
				
		$pass = HtmlInput::gets()
				->setTishi('请输入密码')
				->addStyle('flex',1)
				->addAttrs('autocomplete','off')
				->addStyle('background','none')
				->addStyle('border','none')
				->addStyle('line-height','30px')
				->setType('password')
				->setLen(50)
				->setId('pass');		
		
		$mdiv = HtmlLabel::get()->setBackgroundColor('var(--main-bgcolor)');
		$mdiv->addStyle('border-top', 'var(--border)');
		$mdiv->addStyle('margin-top', '20px');
		$mdiv->addStyle('border-bottom', 'var(--border)');
		$mdiv->addCont(
			HtmlLabel::get()->setDisplay('flex')
			->setValign()
			->addStyle('padding', '15px')
			->addCont(HtmlLabel::get()->addCont('帐号')->setWidth(70)->setAlign('right')->addStyle('margin-right','10px'))
			->addCont($user)
		);
		$mdiv->addCont(
			HtmlLabel::get()->setDisplay('flex')
			->addStyle('border-top', 'var(--border)')
			->setValign()
			->addStyle('padding', '15px')
			->addCont(HtmlLabel::get()->addCont('密码')->setWidth(70)->setAlign('right')->addStyle('margin-right','10px'))
			->addCont($pass)
		);
		
		$btn = HtmlLabel::get()
			->addStyle('margin-top','30px')
			->addCont(
				HtmlButton::gets()
				->setValue('登录')
				->setId('loginbtn')
				->setWidth(150)
				->setHeight(50)
				->setonClick('submitlogin(this)')
			);
		
		$html->addCont($mdiv);
		$html->addCont($btn);
		$html->addCont(HtmlLabel::get()->setId('msgview')->addStyle('margin-top','10px'));
		
		return $html->show();
	}
	
	/**
	*	登录验证
	*/
	public function checkAction()
	{
		$barr = $this->accessControlAllow();
		if($barr)return $barr;
		$pass  		= Request::post('pass');
		$user  		= Request::post('user','', true);
		$cfrom 		= Request::post('cfrom','', true);
		$web   		= Request::post('web','', true);
		$device  	= Request::post('device','', true);
		$yzmval  	= Request::postint('yzmval');
		$barr		= $this->m('systoken')->login($user, $pass, $cfrom, $web, $device, $yzmval);
		if(!$barr['success']){
			$this->m('syslog')->addError('登录错误','['.$user.']'.$barr['msg'].'', array(
				'device' => $device
			));
		}
		return $barr;
	}
	
	/**
	*	获取验证码
	*/
	public function getcodeAction()
	{
		$barr = $this->accessControlAllow();
		if($barr)return $barr;
		
		$device  	= Request::get('device');
		$mobile  	= Jiami::uncrypt(Request::get('mobile'));
		if(!$device || !$mobile)return $this->returnerror('error');
		
		$loginmode 	= Base::getConfig('loginmode');
		$obj 		= smsChajian::get($loginmode==2 ? 'wxqy' : '');
		return $obj->getCode($mobile, $device, 'login');
	}
	
	/**
	*	登录验证
	*/
	public function appinitAction()
	{
		$barr = $this->accessControlAllow();
		if($barr)return $barr;
		$arrs = array(
			'loginyzm'  => 0,
			'title'		=> TITLE,
			'apptheme'	=> '',
			'titleall'	=> TITLE,
		);
		
		return $this->returnsuccess($arrs);
	}
}